package com.vvlive.caibao.smartcity.ui;

import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.os.Handler;
import android.os.Message;
import android.support.v4.widget.ListViewAutoScrollHelper;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.widget.AbsListView;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.vvlive.caibao.smartcity.adapter.LifeOrderMealWaiMAdapter;
import com.vvlive.caibao.smartcity.base.BaseActivity;
import com.vvlive.caibao.smartcity.bean.LifeOrderMealEatCateBean;
import com.vvlive.caibao.smartcity.felm.LifefelmAdapter;
import com.vvlive.caibao.smartcity.felm.PinnedHeaderListView;
import com.vvlive.caibao.smartcity.felm.TestSectionedAdapter;
import com.vvlive.caibao.smartcity.utils.MyRefreshListener;
import com.vvlive.caibao.smartcity.view.CommonActionBar;
import com.vvlive.caibao.smartcity.view.PullToRefreshLayout;
import com.vvlive.caibao.smartcity.view.PullableListView;

import org.kymjs.kjframe.ui.BindView;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import smartcity.vvlive.caibao.com.smartc.R;

/**
 * Created by lenovo on 2015/7/4.
 * 叫外卖详情页
 */
public class LifeEatOutListActivity extends BaseActivity {
    //店铺信息
//    LifeOrderMealStoreBean mMealStoreBean;
    PullableListView mPullableListView;
    //加载的总列表
    List<LifeOrderMealEatCateBean> mBeanList = new ArrayList<>();
    //用于已选的美食列表
    List<LifeOrderMealEatCateBean> mYiXuanBeanList = new ArrayList<>();
    List<LifeOrderMealEatCateBean> mYiXuan = new ArrayList<>();
    //餐厅名
    @BindView(id = R.id.ct_name,click = false)
    TextView mname;
    //选中的美食个数
    @BindView(id = R.id.order_meal_lsitem_num,click = false)
    TextView item_num;
    //起送提示
    @BindView(id = R.id.qisongtishi,click = false)
    TextView qisongtishi;
    //共选了几份
    @BindView(id = R.id.xuanlejifen,click = false)
    TextView gongxuanfenshu;
    //总价
    @BindView(id = R.id.suoxuanzongjia,click = false)
    TextView zongjia;
    //还需要的配送费
    @BindView(id = R.id.lingxufeisongfei,click = false)
    TextView lingxufei;
    //去结算按钮
    @BindView(id = R.id.eat_qujiesuan,click = true)
    Button qujiesuan;
    Intent mIntent;
    //底部三个显示textview
    List<TextView> viewList = new ArrayList<>();
    public static Handler sHandler = new Handler(){
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            if(msg.arg1 == 11){

            }
        }
    };
    //<仿饿了吗listview联动
    private boolean isScroll = true;

    private ListView left_listView;

    private String[] leftStr = new String[]{"星期一","星期二","星期三","星期四","星期五","星期六","星期日","星期二","星期三","星期四","星期五","星期六","星期日"
    ,"星期一","星期二","星期三","星期四","星期五","星期六","星期日","星期二","星期三","星期四","星期五","星期六","星期日"};
    private String[][] rightStr = new String[][]{
            {"星期一  早餐","星期一  午餐","星期一  晚餐"}, {"星期二  早餐","星期二  午餐","星期二  晚餐"},
            {"星期三  早餐","星期三  午餐","星期三  晚餐"}, {"星期四  早餐","星期四  午餐","星期四  晚餐"},
            {"星期五  早餐","星期五  午餐","星期五  晚餐"}, {"星期六  早餐","星期六  午餐","星期六  晚餐"},
            {"星期日  早餐","星期日  午餐","星期日  晚餐"}, {"星期二  早餐","星期二  午餐","星期二  晚餐"},
            {"星期三  早餐","星期三  午餐","星期三  晚餐"}, {"星期四  早餐","星期四  午餐","星期四  晚餐"},
            {"星期五  早餐","星期五  午餐","星期五  晚餐"}, {"星期六  早餐","星期六  午餐","星期六  晚餐"},
            {"星期日  早餐","星期日  午餐","星期日  晚餐"}
          , {"星期一  早餐","星期一  午餐","星期一  晚餐"}, {"星期二  早餐","星期二  午餐","星期二  晚餐"},
            {"星期三  早餐","星期三  午餐","星期三  晚餐"}, {"星期四  早餐","星期四  午餐","星期四  晚餐"},
            {"星期五  早餐","星期五  午餐","星期五  晚餐"}, {"星期六  早餐","星期六  午餐","星期六  晚餐"},
            {"星期日  早餐","星期日  午餐","星期日  晚餐"}, {"星期二  早餐","星期二  午餐","星期二  晚餐"},
            {"星期三  早餐","星期三  午餐","星期三  晚餐"}, {"星期四  早餐","星期四  午餐","星期四  晚餐"},
            {"星期五  早餐","星期五  午餐","星期五  晚餐"}, {"星期六  早餐","星期六  午餐","星期六  晚餐"},
            {"星期日  早餐","星期日  午餐","星期日  晚餐"}};
//    private String[] leftStr = new String[]{"星期一","星期二","星期三","星期四","星期五","星期六","星期日"};
//    private String[][] rightStr = new String[][]{
//            {"星期一  早餐","星期一  午餐","星期一  晚餐"}, {"星期二  早餐","星期二  午餐","星期二  晚餐"},
//            {"星期三  早餐","星期三  午餐","星期三  晚餐"}, {"星期四  早餐","星期四  午餐","星期四  晚餐"},
//            {"星期五  早餐","星期五  午餐","星期五  晚餐"}, {"星期六  早餐","星期六  午餐","星期六  晚餐"},
//            {"星期日  早餐","星期日  午餐","星期日  晚餐"}};
    //仿饿了吗listview联动>
    @Override
    protected int getContentViewRes() {
        return R.layout.life_eat_out_activity;
    }

    @Override
    public void initWidget() {
        super.initWidget();
//        mMealStoreBean = (LifeOrderMealStoreBean) getIntent().getSerializableExtra("dingcan");
        CommonActionBar actionBar = super.getCommonActionBar();
//        actionBar.setTitle(mMealStoreBean.getName());
        actionBar.setTitle("订餐");


        ((PullToRefreshLayout) findViewById(R.id.pulltorefreshlayout))
                .setOnRefreshListener(new MyRefreshListener());
        mPullableListView = (PullableListView) findViewById(R.id.life_buttom_list);
        init();
        //底部三个显示textview
        viewList.add((TextView) findViewById(R.id.xuanlejifen));
        viewList.add((TextView) findViewById(R.id.suoxuanzongjia));
        viewList.add((TextView) findViewById(R.id.lingxufeisongfei));

        LifeOrderMealWaiMAdapter orderMealListAdapter = new LifeOrderMealWaiMAdapter(getApplicationContext(),mBeanList,mYiXuanBeanList,viewList,20);
        mPullableListView.setAdapter(orderMealListAdapter);
        mIntent = new Intent();
        mPullableListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                mIntent.setClass(getApplicationContext(),LifeFoodDetailsActivity.class);
                startActivity(mIntent);
            }
        });
        //仿饿了吗
        fElma();
    }
    @Override
    public void widgetClick(View v) {
        super.widgetClick(v);
        switch (v.getId()){
            case R.id.eat_qujiesuan:
                if(mYiXuanBeanList.size()!=0){
                    Intent intent  = new Intent(this,LifeSubmitEatOrdersActivity.class);
                    intent.putExtra("smtorder", (Serializable) mYiXuanBeanList);
                    startActivity(intent);
                }else{
                    Toast.makeText(getApplicationContext(),"请选择美食",Toast.LENGTH_SHORT).show();
                }
                break;
        }
    }
    /**
     * 仿饿了吗
     * */
    public void fElma(){
        final PinnedHeaderListView right_listview = (PinnedHeaderListView) findViewById(R.id.life_pinnedListView);
        LayoutInflater inflator = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
//        LinearLayout header1 = (LinearLayout) inflator.inflate(R.layout.list_item, null);
//        ((TextView) header1.findViewById(R.id.textItem)).setText("HEADER 1");
//        LinearLayout header2 = (LinearLayout) inflator.inflate(R.layout.list_item, null);
//        ((TextView) header2.findViewById(R.id.textItem)).setText("HEADER 2");
//        LinearLayout footer = (LinearLayout) inflator.inflate(R.layout.list_item, null);
//        ((TextView) footer.findViewById(R.id.textItem)).setText("FOOTER");
        //        listView.addHeaderView(header1);
        //        listView.addHeaderView(header2);
        //        listView.addFooterView(footer);
        final TestSectionedAdapter sectionedAdapter = new TestSectionedAdapter(this, leftStr, rightStr,mBeanList,mYiXuanBeanList,viewList,20);
        right_listview.setAdapter(sectionedAdapter);

        left_listView = (ListView) findViewById(R.id.left_listview);
        LifefelmAdapter left_ = new LifefelmAdapter(this,leftStr);
        left_listView.setAdapter(left_);
        left_listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> arg0, View view, int position,
                                    long arg3) {
                forceScroll();
                isScroll = false;
                //                判断是否是当前选择的item，/是则改变背景颜色
                left_listView.getChildCount();//现实层面上包含的
                left_listView.getCount();//全部的item
                //                for (int i = 0; i < left_listView.getCount(); i++) {
                for (int i = 0; i < left_listView.getChildCount(); i++) {

                    Log.i("00000000000", "00000000000000000000");
                    if (i == position) {
                        Log.i("111111111111", "i:" + i + "    left_listView.getChildAt(i):" + left_listView.getChildAt(i));
                        left_listView.getChildAt(i).setBackgroundColor(Color.rgb(255, 255, 255));
                    } else {
                        Log.i("222222222222", "i:" + i + "    left_listView.getChildAt(i):" + left_listView.getChildAt(i));
                        left_listView.getChildAt(i).setBackgroundColor(Color.TRANSPARENT);
                    }
                }

                int rightSection = 0;
                for (int i = 0; i < position; i++) {
                    rightSection += sectionedAdapter.getCountForSection(i) + 1;
                }
                right_listview.setSelection(rightSection);
            }

        });
        right_listview.setOnScrollListener(new AbsListView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(AbsListView arg0, int arg1) {
                Log.i("滑动=====onScrollStateChanged()=======","   arg1="+arg1);
            }
            @Override
            public void onScroll(AbsListView view, int firstVisibleItem,
                                 int visibleItemCount, int totalItemCount) {
                if (isScroll) {
                    //超过当前显示的最底部，和顶部
                    leftPotn = sectionedAdapter.getSectionForPosition(firstVisibleItem);
                    if(nowPotn<left_listView.getChildCount()-2){
                        nowPotn = leftPotn;
                    }
                    if(nowPotn>left_listView.getChildCount()-2){
                        nowPotn = left_listView.getChildCount()-2;
                    }
                    if (sectionedAdapter.getSectionForPosition(firstVisibleItem) == left_listView.getChildCount()-1) {
                        left_listView.smoothScrollBy(itemHeight, 1);
                        nowPotn = left_listView.getChildCount()-2;
                        Log.i("nowPostion","nowPotn==============================================="+nowPotn);
                    }
//                    Log.i("滑动============","sectionedAdapter.getSectionForPosition(firstVisibleItem)="+sectionedAdapter.getSectionForPosition(firstVisibleItem)+"\n    firstVisibleItem="+firstVisibleItem+"    visibleItemCount="+visibleItemCount+"  totalItemCount="+totalItemCount);
//                    else if (sectionedAdapter.getSectionForPosition(firstVisibleItem) == 0) {
//                        left_listView.smoothScrollBy(-itemHeight, 1);
//                    }
                    Log.i("nowPostion","nowPotn========"+nowPotn);
                    left_listView.getChildAt(lastPotn).setBackgroundColor(Color.TRANSPARENT);
                    left_listView.getChildAt(nowPotn).setBackgroundColor(
                            Color.rgb(255, 255, 255));
                    lastPotn = nowPotn;

//                    for (int i = 0; i < left_listView.getChildCount(); i++) {
//
//                        if (i == sectionedAdapter.getSectionForPosition(firstVisibleItem)) {
//                            left_listView.getChildAt(i).setBackgroundColor(
//                                    Color.rgb(255, 255, 255));
//                        } else {
//                            left_listView.getChildAt(i).setBackgroundColor(Color.TRANSPARENT);
//                        }
                        //                        if(sectionedAdapter.getSectionForPosition(firstVisibleItem)==left_listView.getChildCount()){
                        //                            listScrollUp();
                        //                            list_Pt = i;
                        //                        }
//                    }
                } else {
                    isScroll = true;
                }
            }
        });
        right_listview.setOnItemClickListener(new PinnedHeaderListView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int section, int position, long id) {
                mIntent.setClass(getApplicationContext(), LifeFoodDetailsActivity.class);
                startActivity(mIntent);
            }
            @Override
            public void onSectionClick(AdapterView<?> adapterView, View view, int section, long id) {

            }
        });
        //获取高度
        getListItemHeight();
    }
    /**
     * 自动滑动
     * jdjaskdjjdfna
     * */
    private void forceScroll() {
        MotionEvent event = MotionEvent.obtain(System.currentTimeMillis(), System.currentTimeMillis(), MotionEvent.ACTION_MOVE, left_listView.getHeight() / 9, -1, 0);
        ListViewAutoScrollHelper mScrollHelper = new ListViewAutoScrollHelper(left_listView);
        mScrollHelper.onTouch(left_listView, event);
    }
    /**
     * 刷新buttom,所选份数，总价，另需的配送费
     * */
     private void refreshCate(){

     }
     private void init(){
        mBeanList.add(new LifeOrderMealEatCateBean(R.mipmap.caidetupian, "鱼香肉丝","满25减8",6,3,3,17.00,15.00));
        mBeanList.add(new LifeOrderMealEatCateBean(R.mipmap.caidetupian, "鱼香肉丝","满25减8",6,3,3,17.00,15.00));
        mBeanList.add(new LifeOrderMealEatCateBean(R.mipmap.caidetupian, "鱼香肉丝","满25减8",6,3,3,17.00,15.00));
        mBeanList.add(new LifeOrderMealEatCateBean(R.mipmap.caidetupian, "鱼香肉丝","满25减8",6,3,3,17.00,15.00));
        mBeanList.add(new LifeOrderMealEatCateBean(R.mipmap.caidetupian, "回锅肉","满25减8",6,3,3, 17.00,15.00));
        mBeanList.add(new LifeOrderMealEatCateBean(R.mipmap.caidetupian, "鱼香肉丝","满25减8",6,3,3, 17.00,15.00));
        mBeanList.add(new LifeOrderMealEatCateBean(R.mipmap.caidetupian, "土豆丝","满25减8",6,3,3,17.00,15.00));
        mBeanList.add(new LifeOrderMealEatCateBean(R.mipmap.caidetupian, "鱼香肉丝","满25减8",6,3,3,17.00,15.00));
        mBeanList.add(new LifeOrderMealEatCateBean(R.mipmap.caidetupian, "鱼香肉丝","满25减8",6,3,3,17.00,15.00));
        mBeanList.add(new LifeOrderMealEatCateBean(R.mipmap.caidetupian, "回锅肉","满25减8",6,3,3, 17.00,15.00));
        mBeanList.add(new LifeOrderMealEatCateBean(R.mipmap.caidetupian, "鱼香肉丝","满25减8",6,3,3,17.00,15.00));
        mBeanList.add(new LifeOrderMealEatCateBean(R.mipmap.caidetupian, "鱼香肉丝","满25减8",6,3,3, 17.00,15.00));
        mBeanList.add(new LifeOrderMealEatCateBean(R.mipmap.caidetupian, "回锅肉","满25减8",6,3,3, 17.00,15.00));
        mBeanList.add(new LifeOrderMealEatCateBean(R.mipmap.caidetupian, "鱼香肉丝","满25减8",6,3,3, 17.00,15.00));
        mBeanList.add(new LifeOrderMealEatCateBean(R.mipmap.caidetupian, "鱼香肉丝","满25减8",6,3,3, 17.00,15.00));
        mBeanList.add(new LifeOrderMealEatCateBean(R.mipmap.caidetupian, "回锅肉","满25减8",6,3,3,17.00,15.00));
        mBeanList.add(new LifeOrderMealEatCateBean(R.mipmap.caidetupian, "鱼香肉丝","满25减8",6,3,3,17.00,15.00));
        mBeanList.add(new LifeOrderMealEatCateBean(R.mipmap.caidetupian, "鱼香肉丝","满25减8",6,3,3,17.00,15.00));
        mBeanList.add(new LifeOrderMealEatCateBean(R.mipmap.caidetupian, "鱼香肉丝","满25减8",6,3,3,17.00,15.00));
        mBeanList.add(new LifeOrderMealEatCateBean(R.mipmap.caidetupian, "鱼香肉丝","满25减8",6,3,3,17.00,15.00));
    }
    int itemHeight;     //item项的高度
    int leftPotn = 0;   //当前选中状态
    int lastPotn = 0;       //上一个选中状态
    int nowPotn = 0;        //当前
    /**
     * 获取左边listview高度
     * */
    public void getListItemHeight(){
        ListAdapter listAdapter = left_listView.getAdapter();
        if (listAdapter == null) {
            return;
        }
        View listItem = listAdapter.getView(0, null, left_listView);
        listItem.measure(0, 0);
        itemHeight = listItem.getMeasuredHeight();
    }





    int list_Pt;
    Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            handler.removeCallbacks(run_scroll_down);
            handler.removeCallbacks(run_scroll_up);
        }
    };
    /**
     * 向上滚动
     */
    public void listScrollUp() {
        listScrollOff();
        handler.postDelayed(run_scroll_up, 100);
    }
    /**
     * 向下滚动
     */
    public void listScrollDown() {
        listScrollOff();
        handler.postDelayed(run_scroll_down, 0);
    }
    /**
     * 停止滚动
     */
    public void listScrollOff() {
        handler.removeCallbacks(run_scroll_down);
        handler.removeCallbacks(run_scroll_up);
    }
    Runnable run_scroll_up = new Runnable() {
        @Override
        public void run() {
            /**
             * public void smoothScrollBy (int distance, int duration)
             *
             * Added in API level 8 Smoothly scroll by distance pixels over duration milliseconds.
             *
             * Parameters
             *     distance Distance to scroll in pixels.
             *     duration Duration of the scroll animation in milliseconds.
             */
//            left_listView.smoothScrollBy(100, 1);
            left_listView.scrollTo(list_Pt,list_Pt+1);
            handler.postDelayed(run_scroll_up, 1);
            listScrollOff();
        }
    };
    Runnable run_scroll_down = new Runnable() {
        @Override
        public void run() {
            left_listView.smoothScrollBy(-10, 1);
            handler.postDelayed(run_scroll_down, 1);
        }
    };
}
